<?php
/**
 * JosemUserController.php
 *
 * @copyright   2024 jsjsem.cn - All Rights Reserved
 * @link        https://www.jsjsem.cn/
 * @author      stiffer.chen <service@josen.info>
 * @created     2024-04-19 14:28:07
 * @modified    2024-04-19 14:28:07
 *
 */

namespace App\Http\Controllers;

use App\Services\JosemUserServ;

class JosemUserController extends BaseController
{
    /**
     * @OA\Post(
     *     path="/user/reset_password",
     *     operationId="user_reset_password",
     *     tags={"User"},
     *     summary="忘记密码",
     *     description="忘记密码",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *         description="忘记密码(重置)",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"mobile", "sms_code", "password", "verify_password"},
     *                 @OA\Property(property="mobile", description="手机号码", type="string"),
     *                 @OA\Property(property="sms_code", description="短信验证码", type="string"),
     *                 @OA\Property(property="password", description="密码", type="string"),
     *                 @OA\Property(property="verify_password", description="确认密码", type="string"),
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401, description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function resetPassword()
    {
        $mobile = trim(request()->get('mobile'));
        if (!verify_telephone($mobile)) {
            return message('重置密码失败, 手机号码格式错误', false);
        }
        $smsCode = trim(request()->get('sms_code'));
        $password = trim(request()->get('password'));
        $verifyPassword = trim(request()->get('verify_password'));
        if ($password != $verifyPassword) {
            return message('重置密码失败, 两次密码输入不一致', false);
        }
        try {
            $josemUserServ = new JosemUserServ();
            $josemUserServ->resetPassword($mobile, $smsCode, $password);
            return message('密码重置成功, 请重新登录', true);
        } catch (\Exception $e ){
            return message($e->getMessage(), false);
        }
    }
}